import {courseList} from "../../api/manage"
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    tableList: {
      type: Array,
      value: []
    },
    listText: {
      type: String,
      value: ''
    },
    listName:{
      type:Array,
      value:[]
    },
    totalName:{
      type:Array,
      value:[]
    },
    displayField1: {
      type: String,
      // value: 'studentNo'  
    },
    displayField2: { 
      type: String,
      // value: 'name'  
    },
    displayField3: { 
      type: String,
      value: 'grade' 
    },
    displayField4: { 
      type: String,
      value: 'major'  
    }
  },
 
  /**
   * 组件的初始数据
   */
  data: {
    show: false,
    theItem:{},
    theIndex: 0,
    keys: [],
    list: [],
    result: []
  },

  /**
   * 组件的方法列表
   */
  methods: {
   //点击某一列
   handleClick(event) {
    var item = event.currentTarget.dataset.item; // 获取点击的行数据
    var index = event.currentTarget.dataset.index; // 获取当前项的索引
    var theItem = {};

    // 判断 displayField1 的值，并根据条件构建 theItem
    if (this.data.displayField1 === "teacherNo") {
      theItem = {
        teacherNo: item.teacherNo,
        name: item.name,
        phone: item.phone,
        major: item.major,
        courses:item.courses,
        teacherId:item.teacherId
      };
    } else {
      theItem = {
        studentNo: item.studentNo,
        studentName: item.studentName, // 确保有默认值
        grade: item.grade,
        major: item.major,
        studentId:item.studentId
      };
    }

    this.setData({
      show: true,
      // theItem: item,
      theItem:theItem,
      theIndex: index, // 存储索引
      // keys: Object.keys(item),
      result: item.courses || [],
      keys: Object.keys(theItem), // 获取 theItem 的所有键
      // result: theItem.courses || []
    });
    console.log('theItem:', this.data.theItem.courses); // 输出到控制台
  },
    //点击确定
    getUserInfo() {
      console.log(this.data.theItem); // 打印更新后的数据
      this.updateTableItem(this.data.theItem)
    },
    updateTableItem(newData) {
      console.log("点击了更新updateTableItem",newData);
      this.triggerEvent('tableUpdate', { updatedItem: newData, updateIndex: this.data.theIndex });
    },
    //点击删除
    onClose() {
      this.setData({ close: false });
      this.delectTableItem(this.data.theIndex)
    },
    onInputChange(event) {
      const field = event.currentTarget.dataset.field; // 获取字段名
      const value = event.detail; // 获取输入的值
      console.log("onInputChange:");
      // 更新 theItem 对象
      this.setData({
        [`theItem.${field}`]: value // 动态更新
      });
    },
    delectTableItem(theIndex){
      console.log("点击了删除");
      this.triggerEvent('tableDelect', {updateIndex: theIndex });
    },

    onChange(event) {
      this.setData({
        result: event.detail,
        "theItem.courses":event.detail
      });
      console.log(this.data.result);
    },
    async fetchCourseList() {
      let res = await courseList();
      console.log("attached:", res.data);
      
      // 提取 courseName 并存储到 result 数组
      const courseNames = res.data.map(course => course.courseName);
      
      this.setData({
        list: courseNames // 存放 courseName 的数组
      });
    }

  
 
  },
  attached() {
    this.fetchCourseList();
  },
})